package com.pine.manager.pineframework.trigger.impl;

import com.pine.common.beans.exception.BusinessException;
import com.pine.common.beans.exception.ExceptionCodeEnum;
import com.pine.common.beans.pineframework.request.SysDeptSaveOrUpdateRequest;
import com.pine.common.trigger.TriggerContext;
import com.pine.common.util.user.UserInfo;
import com.pine.common.util.user.UserUtil;
import com.pine.dao.pineframework.entity.SysDept;
import com.pine.manager.core.TriggerTypeEnum;
import com.pine.manager.pineframework.bo.SysDeptBo;
import com.pine.manager.pineframework.query.SysDeptQuery;
import com.pine.manager.pineframework.trigger.AbstractTriggerTemplateAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 系统部门表新增或修改数据 触发器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-16
 */
@Component
@Slf4j
public class SysDeptSaveOrUpdateTrigger extends AbstractTriggerTemplateAdapter<SysDeptSaveOrUpdateRequest, Boolean> {

    private boolean isSave;

    private SysDeptBo oldSysDeptBo;

    public SysDeptSaveOrUpdateTrigger() {
        super(TriggerTypeEnum.SYS_DEPT_SAVE_OR_UPDATE);
    }

    @Override
    protected void before(TriggerContext<SysDeptSaveOrUpdateRequest> triggerContext) {
        SysDeptSaveOrUpdateRequest request = triggerContext.getTriggerRequest();
        isSave = Objects.isNull(request.getId());

        // 尝试查询旧部门数据
        SysDeptQuery query = new SysDeptQuery();
        query.setId(request.getId());
        if (isSave) {
            query.setCode(request.getCode());
        }
        oldSysDeptBo = sysDeptManager.getOne(query);

        // 校验
        if (isSave) {
            checkOnSave();
        } else {
            checkOnUpdate();
        }
    }

    /**
     * 触发
     *
     * @param triggerContext 触发上下文
     * @return 触发结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean trigger(TriggerContext<SysDeptSaveOrUpdateRequest> triggerContext) {
        // 获取请求参数
        SysDeptSaveOrUpdateRequest request = triggerContext.getTriggerRequest();

        // 转换为业务对象，并设置额外字段
        SysDeptBo newSysDeptBo = sysDeptConvertor.saveRequestToEntityBo(request);
        setUserInfo(newSysDeptBo);

        // 转换为实体
        SysDept sysDept = sysDeptConvertor.entityBoToEntity(newSysDeptBo);

        // 新增
        if (isSave) {
            return sysDeptManager.save(sysDept);
        }

        // 修改
        return sysDeptManager.updateById(sysDept);
    }

    private void checkOnSave() {
        // 校验部门是否重复
        if (Objects.nonNull(oldSysDeptBo)) {
            throw new BusinessException(ExceptionCodeEnum.DEPT_EXIST);
        }
    }

    private void checkOnUpdate() {
        // 校验部门是否存在
        if (Objects.isNull(oldSysDeptBo)) {
            throw new BusinessException(ExceptionCodeEnum.DEPT_NOT_EXIST);
        }
    }

    private void setUserInfo(SysDeptBo sysDeptBo) {
        UserInfo userInfo = UserUtil.get();
        if (Objects.nonNull(userInfo)) {
            if (isSave) {
                sysDeptBo.setCreateUserId(userInfo.id());
            }
            sysDeptBo.setUpdateUserId(userInfo.id());
        }
    }
}
